# -*- coding: utf-8 -*-

from django.db import connection



################################################################################
def dict_fetchall(cursor):
    #---------------------------------------------------------------------------
    columns = [col[0] for col in cursor.description]
    return [
        dict(zip(columns, row))
        for row in cursor.fetchall()
    ]
    #---------------------------------------------------------------------------



################################################################################
def dict_fetchone(cursor):
    #---------------------------------------------------------------------------
    results = dict_fetchall(cursor)
    if results: return results[0]
    else: return None
    #---------------------------------------------------------------------------



################################################################################
cursor = None
#-------------------------------------------------------------------------------
def dbConnect():
    #---------------------------------------------------------------------------
    global cursor
    cursor = connection.cursor()
    #---------------------------------------------------------------------------



################################################################################
def dbExecute(query):
    #---------------------------------------------------------------------------
    cursor.execute(query)
    #---------------------------------------------------------------------------



################################################################################
def dbResult(query):
    #---------------------------------------------------------------------------
    cursor.execute(query)
    return cursor.fetchone()[0]
    #---------------------------------------------------------------------------



################################################################################
def dbRow(query):
    #---------------------------------------------------------------------------
    cursor.execute(query)
    return dict_fetchone(cursor)
    #---------------------------------------------------------------------------



################################################################################
def dbRows(query):
    #---------------------------------------------------------------------------
    cursor.execute(query)
    return dict_fetchall(cursor)
    #---------------------------------------------------------------------------



################################################################################
def strSql(value):
    #---------------------------------------------------------------------------
    if value == None: return 'NULL'
    #---------------------------------------------------------------------------
    result = str(value).replace('\\', '\\\\') 
    result = result.replace('\'', '\'\'')
    result = '\'' + result + '\''
    return result
    #---------------------------------------------------------------------------
